package com.example.biji.util;



import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.biji.model.Note;
import com.example.biji.model.User;

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "notes_app.db";
    private static final int DATABASE_VERSION = 1;

    // 用户表创建语句
    private static final String CREATE_USER_TABLE = "CREATE TABLE " + User.TABLE_NAME + " (" +
            User.COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            User.COLUMN_USERNAME + " TEXT NOT NULL UNIQUE, " +
            User.COLUMN_PASSWORD + " TEXT NOT NULL, " +
            User.COLUMN_CREATE_TIME + " TEXT);";

    // 笔记表创建语句
    private static final String CREATE_NOTE_TABLE = "CREATE TABLE " + Note.TABLE_NAME + " (" +
            Note.COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            Note.COLUMN_TITLE + " TEXT NOT NULL, " +
            Note.COLUMN_CONTENT + " TEXT, " +
            Note.COLUMN_CREATE_TIME + " TEXT, " +
            Note.COLUMN_UPDATE_TIME + " TEXT, " +
            Note.COLUMN_USER_ID + " INTEGER, " +
            "FOREIGN KEY (" + Note.COLUMN_USER_ID + ") REFERENCES " +
            User.TABLE_NAME + "(" + User.COLUMN_ID + "));";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_USER_TABLE);
        db.execSQL(CREATE_NOTE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + User.TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + Note.TABLE_NAME);
        onCreate(db);
    }
}